home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Plus 1996 #3
/
Amiga Plus CD - 1996 - No. 3.iso
/
pd
/
daten
/
routeplanner_v1.6
/
routeedit_mem.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-02-20
|
9KB
|
423 lines
// RouteEdit_mem.c -- edits RoutePlanner files
#include <proto/asl.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/icon.h>
#include <proto/utility.h>
#include <proto/muimaster.h>
#include <proto/intuition.h>
#include <clib/alib_protos.h>
#include <exec/memory.h>
#include <utility/hooks.h>
#include <libraries/mui.h>
#include <libraries/gadtools.h>
#include "RoutePlanner.h"
#include "RouteEdit_rev.h"
#include <dos.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <reg.h>
extern struct Library *MUIMasterBase;
extern struct Library *IconBase;
extern struct Library *UtilityBase;
extern struct IntuitionBase *IntuitionBase;
extern struct Library *AslBase;
extern BOOL Authenticated;
extern APTR app, memptr, cdptr, rdptr, mainwindow;
extern struct DiskObject *disko;
// These two pointers used by memory reallocation system
extern struct CityData *cd;
extern struct RouteData *rd;
extern ULONG cdcount,rdcount;
extern struct DataBaseInfo dbi;
extern struct Registration *reg;
extern UWORD MeasureUnits;
BOOL OpenDatabase(char * );
BOOL SaveDatabase(char * );
BOOL RedoMemory(ULONG , ULONG );
void ReLink(void);
BOOL OpenDatabase(char *filename)
{
struct DataBaseInfo *dbitmp;
APTR tmemptr;
BPTR fhandle;
__aligned struct FileInfoBlock fib;
ULONG *temp;
fhandle=Open(filename,MODE_OLDFILE);
if(!fhandle) return FALSE;
if(!ExamineFH(fhandle,&fib))
{
Close(fhandle);
return FALSE;
}
tmemptr=malloc(fib.fib_Size);
if(!tmemptr)
{
Close(fhandle);
return FALSE;
}
FRead(fhandle,tmemptr,1,fib.fib_Size);
Close(fhandle);
temp=(ULONG *)tmemptr;
if((*temp)!='RPL2')
{
return FALSE;
}
RedoMemory(0L,0L);
memptr=tmemptr;
cdptr=NULL;
rdptr=NULL;
cd=(struct CityData *)((char *)memptr+12);
temp=(ULONG *)((char *)(cd)-4);
cdcount=*temp;
rd=(struct RouteData *)
((char *)memptr+(sizeof(struct CityData)*(cdcount)+20));
temp=(ULONG *)((char *)(rd)-4);
rdcount=*temp;
dbitmp=(struct DataBaseInfo *)((char *)memptr+20+
(sizeof(struct CityData)*cdcount)+(sizeof(struct RouteData)*rdcount));
memcpy(&dbi,dbitmp,sizeof(dbi));
if(reg)
{
if((dbi.dbi_AuthorCode!=(ULONG)~0 && dbi.dbi_AuthorCode!=reg->ActualNumber)
|| dbi.dbi_Security!=DoFunkyThing(cdcount+rdcount+dbi.dbi_AuthorCode+
StringSum(dbi.dbi_Author)+StringSum(dbi.dbi_EMail)))
{
RedoMemory(0L,0L);
cdcount=0;
rdcount=0;
memset(&dbi,'\0',sizeof(dbi));
MUI_Request(app,mainwindow,0,NULL,"_Ok",
"You may not open this database.");
}
}
else
{
if(dbi.dbi_AuthorCode!=(ULONG)~0 ||
dbi.dbi_Security!=DoFunkyThing(cdcount+rdcount+dbi.dbi_AuthorCode+
StringSum(dbi.dbi_Author)+StringSum(dbi.dbi_EMail)))
{
RedoMemory(0L,0L);
cdcount=0;
rdcount=0;
memset(&dbi,'\0',sizeof(dbi));
MUI_Request(app,mainwindow,0,NULL,"_Ok",
"You may not open this database.");
}
}
ReLink();
return TRUE;
}
BOOL SaveDatabase(char *filename)
{
BPTR fhandle;
ULONG id='RPL2', cid='CITY', rid='ROAD';
LONG err, i;
static char buffer[100], *header="Error writing file";
struct DiskObject *tdisko;
fhandle=Open(filename,MODE_NEWFILE);
if(!fhandle) return FALSE;
SetIoErr(0L);
FWrite(fhandle,&id,sizeof(id),1);
err=IoErr();
if(err)
{
Fault(err,header,buffer,sizeof(buffer));
MUI_Request(app,mainwindow,0,NULL,"_Ok",buffer);
Close(fhandle);
return FALSE;
}
SetIoErr(0L);
FWrite(fhandle,&cid,sizeof(cid),1);
err=IoErr();
if(err)
{
Fault(err,header,buffer,sizeof(buffer));
MUI_Request(app,mainwindow,0,NULL,"_Ok",buffer);
Close(fhandle);
return FALSE;
}
SetIoErr(0L);
FWrite(fhandle,&cdcount,sizeof(cdcount),1);
err=IoErr();
if(err)
{
Fault(err,header,buffer,sizeof(buffer));
MUI_Request(app,mainwindow,0,NULL,"_Ok",buffer);
Close(fhandle);
return FALSE;
}
for(i=0;i<cdcount;i++)
{
cd[i].RoadList=NULL;
cd[i].Colum=0;
cd[i].cd_Dummy=0;
}
SetIoErr(0L);
FWrite(fhandle,&cd[0],sizeof(struct CityData),cdcount);
err=IoErr();
if(err)
{
Fault(err,header,buffer,sizeof(buffer));
MUI_Request(app,mainwindow,0,NULL,"_Ok",buffer);
ReLink();
Close(fhandle);
return FALSE;
}
SetIoErr(0L);
FWrite(fhandle,&rid,sizeof(rid),1);
err=IoErr();
if(err)
{
Fault(err,header,buffer,sizeof(buffer));
MUI_Request(app,mainwindow,0,NULL,"_Ok",buffer);
ReLink();
Close(fhandle);
return FALSE;
}
SetIoErr(0L);
FWrite(fhandle,&rdcount,sizeof(rdcount),1);
err=IoErr();
if(err)
{
Fault(err,header,buffer,sizeof(buffer));
MUI_Request(app,mainwindow,0,NULL,"_Ok",buffer);
ReLink();
Close(fhandle);
return FALSE;
}
for(i=0;i<rdcount;i++)
{
rd[i].CityNum[0]=NULL;
rd[i].CityNum[1]=NULL;
rd[i].RLink[0]=NULL;
rd[i].RLink[1]=NULL;
}
SetIoErr(0L);
FWrite(fhandle,&rd[0],sizeof(struct RouteData),rdcount);
err=IoErr();
if(err)
{
Fault(err,header,buffer,sizeof(buffer));
MUI_Request(app,mainwindow,0,NULL,"_Ok",buffer);
ReLink();
Close(fhandle);
return FALSE;
}
SetIoErr(0L);
FWrite(fhandle,&dbi,sizeof(dbi),1);
err=IoErr();
if(err)
{
Fault(err,header,buffer,sizeof(buffer));
MUI_Request(app,mainwindow,0,NULL,"_Ok",buffer);
ReLink();
Close(fhandle);
return FALSE;
}
ReLink();
Close(fhandle);
tdisko=GetDiskObject(filename);
if(!tdisko)
{
tdisko=GetDiskObjectNew("PROGDIR:RoutePlanner");
if(tdisko)
{
tdisko->do_Type=WBPROJECT;
tdisko->do_DefaultTool="RoutePlanner";
tdisko->do_CurrentX=NO_ICON_POSITION;
tdisko->do_CurrentY=NO_ICON_POSITION;
PutDiskObject(filename,tdisko);
}
else
{
tdisko=GetDefDiskObject(WBPROJECT);
if(tdisko)
{
tdisko->do_Type=WBPROJECT;
tdisko->do_DefaultTool="RoutePlanner";
tdisko->do_CurrentX=NO_ICON_POSITION;
tdisko->do_CurrentY=NO_ICON_POSITION;
PutDiskObject(filename,tdisko);
}
}
}
if(tdisko) FreeDiskObject(tdisko);
return TRUE;
}
BOOL RedoMemory(ULONG NeededCityEntries, ULONG NeededRouteEntries)
{
APTR temp1, temp2;
BOOL ok;
if(NeededCityEntries==0 && NeededRouteEntries==0)
{
if(memptr)
{
free(memptr);
memptr=NULL;
}
else
{
if(rdptr) free(rdptr);
if(cdptr) free(cdptr);
cdptr=NULL;
rdptr=NULL;
}
cd=NULL;
rd=NULL;
ok=TRUE;
}
else if(NeededCityEntries!=cdcount || NeededRouteEntries!=rdcount)
{
if(memptr)
{
ULONG csize, rsize;
csize=min(cdcount,NeededCityEntries)*sizeof(struct CityData);
rsize=min(rdcount,NeededRouteEntries)*sizeof(struct RouteData);
temp1=malloc(NeededCityEntries*sizeof(struct CityData));
temp2=malloc(NeededRouteEntries*sizeof(struct RouteData));
if((temp1 || cdcount==0) && (temp2 || rdcount==0))
{
CopyMem(cd,temp1,csize);
CopyMem(rd,temp2,rsize);
free(memptr);
memptr=NULL;
cdptr=temp1;
rdptr=temp2;
ok=TRUE;
}
else
{
if(temp1) free(temp1);
if(temp2) free(temp2);
ok=FALSE;
}
}
else
{
if(NeededCityEntries!=cdcount)
temp1=realloc(cdptr,NeededCityEntries*sizeof(struct CityData));
else
temp1=cdptr;
if(NeededRouteEntries!=rdcount)
temp2=realloc(rdptr,NeededRouteEntries*sizeof(struct RouteData));
else
temp2=rdptr;
if((temp1 || cdcount==0) && (temp2 || rdcount==0))
{
cdptr=temp1;
rdptr=temp2;
ok=TRUE;
}
else
{
ok=FALSE;
}
}
cd=cdptr;
rd=rdptr;
}
return ok;
}
void ReLink(void)
{
char col;
ULONG i;
struct RouteData *Route;
struct CityData *FoundCity;
for(i=0;i<cdcount;i++)
cd[i].RoadList=NULL;
for(i=0;i<rdcount;i++)
{
Route=&(rd[i]);
for(col=0;col<2;col++)
{
FoundCity=&(cd[Route->CityID[col]]);
Route->CityNum[col]=FoundCity;
Route->RLink[col]=FoundCity->RoadList;
Route->Colm[col]=FoundCity->Colum;
FoundCity->RoadList=Route;
FoundCity->Colum=col;
}
}
}